package com.metaera.app.ui.activities;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;


import com.metaera.app.R;
import com.metaera.app.controller.presenter.PhoneCodePresenter;
import com.metaera.app.ui.adapters.PhoneNumberPickAdapter;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.TextWatcherAdapter;

import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;


/**
 * 手机区号
 */
public class PhoneNumberPickActivity extends BaseActivity {

    RecyclerView recyclerView;
    TextView     txt_search_right;
    EditText     et_search;

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_pick;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        recyclerView = findViewById(R.id.rv_pick);
        txt_search_right = findViewById(R.id.txt_search_right);
        et_search = findViewById(R.id.et_search);
        findViewById(R.id.txt_search_right).setOnClickListener(v -> finish());

        // init view
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
        Drawable drawable = ContextCompat.getDrawable(recyclerView.getContext(), R.drawable.shape_item_divider);
        dividerItemDecoration.setDrawable(Objects.requireNonNull(drawable));
        recyclerView.addItemDecoration(dividerItemDecoration);

        // network
        PhoneCodePresenter presenter = new PhoneCodePresenter();
        presenter.setModelAndView(data -> recyclerView.setAdapter(new PhoneNumberPickAdapter(data)));
        presenter.execute();

        // listener
        et_search.addTextChangedListener(new TextWatcherAdapter(){

            @Override
            public void onTextChanged(@NonNull CharSequence s, int start, int before, int count) {
                presenter.search(s);
            }

        });

    }

}
